<template>
  <div>

    <div class="qup-title-box"><span class="border" /><span>任务基本信息</span></div>
    <base-info ref="refBase" is-detail :decorate-task-no="detailData.decorateTaskNo" :shop-code="detailData.shopCode" />

    <div class="qup-title-box"><span class="border" /><span>巡检申请信息</span></div>
    <application-info ref="refApplication" is-detail :detail-data="detailData" />

    <div class="qup-title-box"><span class="border" /><span>巡检问题信息</span></div>
    <problem-info
      ref="refProblem"
      is-detail
      :schedule="detailData.schedule"
      :config-id-before="problemBeforeId"
      :config-id-after="problemAfterId"
      :patrol-id="detailData.id||patrolId"
    />

    <div class="qup-title-box"><span class="border" /><span>附件信息</span></div>
    <files-info :id="detailData.id||patrolId" ref="refFiles" />
    <NodeList />
    <div class="qup-title-box"><span class="border" /><span>意见信息</span></div>
    <Opinion :id="detailData.id||patrolId" ref="refOpinion" :type="$route.query.type" />
    <div class="footer-btn">
      <el-button :loading="saveLoading" type="primary" size="mini" @click="handelSave(0)">保存</el-button>
      <el-button :loading="saveLoading" type="warning" size="mini" @click="handelSave(1)">提交</el-button>
      <el-button size="mini" @click.native="handleBack">返回</el-button>
    </div>
  </div>
</template>
<script>
import BackPageMixin from '@/mixins/backPage'
import BaseInfo from './components/BaseInfo.vue'
import ApplicationInfo from './components/ApplicationInfo.vue'
import ProblemInfo from './components/ProblemInfo.vue'
import FilesInfo from './components/FilesInfo.vue'
import NodeList from './components/NodeList.vue'
import Opinion from './components/Opinion.vue'
import { formaterData } from '@/utils'
import { getDictByPatchType } from '@/api/dataDict'
import { queryNodeList, saveDailyPatrolFile } from '@/api/marketService/dailyPatrol'
import { getDetailById, postConfigAttachmentTemplate, saveAudit } from '@/api/marketService/decorateInspection'

export default {
  name: 'AuditApply',
  components: { BaseInfo, ApplicationInfo, ProblemInfo, FilesInfo, Opinion, NodeList },
  mixins: [BackPageMixin],
  props: {
  },
  data() {
    return {
      formaterData,
      saveLoading: false,
      patrolId: '',
      tableData: [],
      stepList: [],
      joinerConfigAttachmentId: '',
      problemBeforeId: '',
      problemAfterId: '',
      detailData: {}
    }
  },
  async created() {
    const { id } = this.$route.query
    this.patrolId = id
    this.handleQueryNodeList()
    if (id) {
      getDetailById(id).then(res => {
        this.detailData = res.data
      })
    }
    postConfigAttachmentTemplate().then(res => {
      if (res.success === 1 && res.data.records.length > 0) {
        // 根据belongModel获取每个配置id
        const tempArr = res.data.records.filter(el => el.enable === 1)
        tempArr.map(el => {
          if (el.attachmentType === '参与人合照/现场照片') this.joinerConfigAttachmentId = el.id
          if (el.attachmentType === '巡检问题(前)') this.problemBeforeId = el.id
          if (el.attachmentType === '巡检问题(后)') this.problemAfterId = el.id
        })
      }
    })
    await getDictByPatchType(['DAILY_STATUS']).then(res => {
      this.stepList = res.DAILY_STATUS
    })
  },
  methods: {
    handleQueryNodeList() {
      queryNodeList(this.patrolId).then(res => {
        if (res.success) {
          this.tableData = res.data.records
        }
      })
    },
    handleBack() {
      this.$store.dispatch('views/goBack')
    },
    async handelSave(type = 0) {
      const fileData = this.$refs.refFiles.handleSave()
      const formData = await this.$refs.refOpinion.getData()
      if (fileData.length) {
        this.saveLoading = true
        saveDailyPatrolFile(fileData).then(res => {
          if (res.success)console.log('附件保存成功')
        })// 单独保存附件
      }

      if (type === 1) {
        const problemData = this.$refs.refProblem.getIds(type)
        if (!problemData) {
          this.$message.warning('有未整改问题，请先整改后在提交')
          return
        }
        saveAudit(formData).then(res => {
          if (res.success) {
            this.$message.success('提交成功')
            this.handleBack()
          }
        }).catch(() => {
          this.saveLoading = false
        })
      } else {
        this.$message.success('附件保存成功')
        this.handleBack()
      }
      this.saveLoading = false
    }
  }
}
</script>
<style lang="scss" scoped>
.footer-btn{
      position: fixed;
      left: 0;
      right: 2px;
      bottom: 0;
      padding: 10px;
      margin-top: 20px;
      border-top: 1px solid #eee;
      background-color: #fff;
      z-index: 9;
      box-shadow:0 0 4px #eee;
      text-align: center;
      .el-button+.el-button{
        margin-left: 20px;
      }
    }
</style>
